﻿#include "NetworkDebugWidget.h"

namespace Core {

	NetworkDebugWidget::NetworkDebugWidget(QWidget *parent)
		: QWidget(parent)
		, m_iNetworkType(0)
		, m_pTcpServerController(nullptr)
		, m_pTcpClientController(nullptr)
		, m_pUdpUnicastController(nullptr)
		, m_pUdpBroadcastController(nullptr)
		, m_pUdpMulticastController(nullptr)
	{
		ui.setupUi(this);

		this->initUI();
	}

	NetworkDebugWidget::~NetworkDebugWidget()
	{
		if (m_pTcpServerController != nullptr)
		{
			delete m_pTcpServerController;
			m_pTcpServerController = nullptr;
		}

		if (m_pTcpClientController != nullptr)
		{
			delete m_pTcpClientController;
			m_pTcpClientController = nullptr;
		}

		if (m_pUdpUnicastController != nullptr)
		{
			delete m_pUdpUnicastController;
			m_pUdpUnicastController = nullptr;
		}

		if (m_pUdpBroadcastController != nullptr)
		{
			delete m_pUdpBroadcastController;
			m_pUdpBroadcastController = nullptr;
		}

		if (m_pUdpMulticastController != nullptr)
		{
			delete m_pUdpMulticastController;
			m_pUdpMulticastController = nullptr;
		}
	}

	void NetworkDebugWidget::slotNetworkTypeCurrentlndexChanged(int index)
	{
		// 服务器
		if (index == 0)
		{
			ui.label_ip->setText(u8"本地主机地址");
			ui.label_port->setText(u8"本地主机端口");

		}
		// 客户端
		else if(index == 1)
		{
			ui.label_ip->setText(u8"远程主机地址");
			ui.label_port->setText(u8"远程主机端口");

		}
		// 单播
		else if (index == 2)
		{
		
		}
		// 组播
		else if (index == 3)
		{
	
		}
		// 广播
		else if (index == 4)
		{
		
		}

		m_iNetworkType = index;
	}

	void NetworkDebugWidget::slotNetworkControlBtnClicked()
	{
		QPushButton * btn = static_cast<QPushButton*>(sender());
		if (btn == ui.pushButton_open)
		{
			QString strIp = ui.lineEdit_ip->text();
			int iPort = ui.spinBox_port->value();

			bool bCreateState = false;
			if (m_iNetworkType == 0)
			{
				bCreateState = m_pTcpServerController->setCreateServer(strIp, iPort);
			}
			else if (m_iNetworkType == 1)
			{
				bCreateState = m_pTcpClientController->setConnectToServer(strIp, iPort);
			}
			else if (m_iNetworkType == 2)
			{

			}
			else if (m_iNetworkType == 3)
			{

			}
			else if (m_iNetworkType == 4)
			{

			}

			if (bCreateState)
			{
				ui.pushButton_open->setEnabled(false);
				ui.pushButton_close->setEnabled(true);
			}
		}
		else if (btn == ui.pushButton_close)
		{
			if (m_iNetworkType == 0)
			{
				m_pTcpServerController->setCloseServer();
			}
			else if (m_iNetworkType == 1)
			{
				m_pTcpClientController->setDisconnectFromServer();
			}
			else if (m_iNetworkType == 2)
			{

			}
			else if (m_iNetworkType == 3)
			{

			}
			else if (m_iNetworkType == 4)
			{

			}

			ui.pushButton_open->setEnabled(true);
			ui.pushButton_close->setEnabled(false);
		}
	}

	void NetworkDebugWidget::slotSendMsgTypeButtonClicked(int id)
	{

	}

	void NetworkDebugWidget::slotRecvMsgTypeButtonClicked(int id)
	{

	}

	void NetworkDebugWidget::slotRepeatSendCheckBoxStateChanged(int state)
	{

	}

	void NetworkDebugWidget::slotNetworkSendMsgBtnClicked()
	{
	}

	void NetworkDebugWidget::slotNetworkMsgInfo(int iMsgType, QString strMsg)
	{

	}

	void NetworkDebugWidget::initUI()
	{
		m_pTcpServerController = new TcpServerController(this);
		m_pTcpClientController = new TcpClientController();

		m_pUdpUnicastController = new UdpUnicastController(this);
		m_pUdpBroadcastController = new UdpBroadcastController(this);
		m_pUdpMulticastController = new UdpMulticastController(this);

		{
			ui.radioButton_sendSetASCII->setChecked(true);

			m_pSendButtonGroup = new QButtonGroup(this);
			m_pSendButtonGroup->addButton(ui.radioButton_sendSetASCII, 0);
			m_pSendButtonGroup->addButton(ui.radioButton_sendSetHex, 1);
			m_pSendButtonGroup->setExclusive(true);
			connect(m_pSendButtonGroup, QOverload<int>::of(&QButtonGroup::buttonClicked),
				[this](int id) {
				slotSendMsgTypeButtonClicked(id);
			});
		}

		{
			ui.radioButton_recvSetASCII->setChecked(true);

			m_pRecvButtonGroup = new QButtonGroup(this);
			m_pRecvButtonGroup->addButton(ui.radioButton_recvSetASCII, 0);
			m_pRecvButtonGroup->addButton(ui.radioButton_recvSetHex, 1);
			m_pRecvButtonGroup->setExclusive(true);

			connect(m_pRecvButtonGroup, QOverload<int>::of(&QButtonGroup::buttonClicked),
				[this](int id) {
				slotRecvMsgTypeButtonClicked(id);
			});
		}


		connect(ui.comboBox_type, SIGNAL(currentIndexChanged(int)), this, SLOT(slotNetworkTypeCurrentlndexChanged(int)));

		connect(ui.pushButton_open, &QPushButton::clicked, this, &NetworkDebugWidget::slotNetworkControlBtnClicked);
		connect(ui.pushButton_close, &QPushButton::clicked, this, &NetworkDebugWidget::slotNetworkControlBtnClicked);
		connect(ui.pushButton_sendMsg, &QPushButton::clicked, this, &NetworkDebugWidget::slotNetworkSendMsgBtnClicked);


		slotNetworkTypeCurrentlndexChanged(ui.comboBox_type->currentIndex());

		ui.pushButton_open->setEnabled(true);
		ui.pushButton_close->setEnabled(false);
	}

}//namespace Core